<?php
$breadcrumb = !empty($member->id) ? 'Update Information' : 'Sign In';
$this->breadcrumbs = array(
    $breadcrumb
);
?>

<form method="post" id="sign-in" action="<?php echo Yii::app()->createUrl('/site/sign'); ?>" onsubmit="return checkForm()" class="form-horizontal">
    <?php echo CHtml::hiddenField('id', $member->id); ?>
    <?php if (!$member->id): ?>
        <div class="control-group">
            <?php echo CHtml::label('E-mail*', 'User_mail', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo CHtml::textField('User[mail]', $member->mail, array('size' => 50, 'rel' => 'mail', 'rel-value' => 'Please enter Email!')); ?>
            </div>
        </div>
        <div class="control-group">
            <?php echo CHtml::label('Password*', 'User_password', array('class' => 'control-label')); ?>
            <div class="controls">
                <?php echo CHtml::passwordField('User[password]', '', array('size' => 30, 'rel' => 'required', 'rel-value' => 'Please enter Password!')); ?>
                <?php echo CHtml::passwordField('User[confirm_password]', '', array('size' => 30, 'rel' => 'required', 'rel-value' => 'Please Confirm Password!')); ?>
            </div>
        </div>
    <?php endif; ?>
    <div class="control-group">
        <?php echo CHtml::label('Phone*', 'User_phone', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[phone]', $member->phone, array('size' => 40, 'rel' => 'required', 'rel-value' => 'Please Enter Phone!')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('First name *', 'User_fast_name', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[fast_name]', $member->fast_name, array('size' => 20, 'rel' => 'required', 'rel-value' => 'Please Enter First name!')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('Last name *', 'User_last_name', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[last_name]', $member->last_name, array('size' => 20, 'rel' => 'required', 'rel-value' => 'Please Enter Last name!')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('Country*', 'User_country', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php
            echo CHtml::dropDownList('User[country]', $member->country, array('' => 'Select you country') + CHtml::listData(UsCity::model()->findAll('parentid=0'), 'name', 'name'));
            ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('City*', 'User_city', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[city]', $member->city, array('rel' => 'required', 'rel-value' => 'Please Enter City!')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('Address*', 'User_address', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[address]', $member->address, array('rel' => 'required', 'rel-value' => 'Please Enter Address!', 'class' => 'input-xlarge')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('Zip/Postal code', 'User_zip_code', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[zip_code]', $member->zip_code, array('size' => 20, 'rel-value' => 'Please Enter Zip Code!')); ?>
        </div>
    </div>
    <div class="control-group">
        <?php echo CHtml::label('MSN', 'User_msn', array('class' => 'control-label')); ?>
        <div class="controls">
            <?php echo CHtml::textField('User[msn]', $member->msn, array('size' => 20, 'rel-value' => 'Please Enter msn!')); ?>
        </div>
    </div>
    <div class="action">
        <?php
        if ($member->id)
            $bString = "Update";
        else
            $bString = "Sign In";
        echo CHtml::submitButton($bString, array('class' => 'btn btn-primary btn-large pull-right'));
        ?>
    </div>
</form>
<div class="clearfix"></div>


<script>
    function checkForm(){
        var sub = false;
        $.each($("#sign-in input"), function(n, i){
            switch($(i).attr("rel")){
                case 'required':
                    if($(i).val()==""){
                        alert($(i).attr("rel-value"));
                        $(i).focus();
                        sub = false;
                        return false;
                    }else{
                        sub = true;
                    }
                    break;
                case 'mail':
                    if(!isEmail($(i).val())){
                        alert('Please enter Email!');
                        $(i).focus();
                        return false;
                        sub = false;
                    }else{
                        sub = true;
                    }
                    break;
            }
        });
        if(sub === false){
            return false;
        }
        $.each($("#sign-in select"), function(n, i){
            switch($(i).attr("rel")){
                case 'required':
                    if($(i).val()==""){
                        alert($(i).attr("rel-value"));
                        $(i).focus();
                        sub = false;
                        return false;
                    }else{
                        sub = true;
                    }
                    break;
            }
        });
        if('<?php echo $member->id ?>' == ''){
            if($("#User_password").val()!==$("#User_confirm_password").val()){
                alert("Confirm Password error!");
                sub = false;
                return false;
            }
        }
        return sub;
    }
    function isEmail(str){
        res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+[\.]{1}([0-9a-zA-Z_\-\.]+)+$/;
        var re = new RegExp(res);
        return !(str.match(re) == null);
    }
</script>